package database;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;

public class DatabaseTest extends JFrame {
	
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@192.168.7.11:1521:xe";
	String db_user = "hr";
	String db_pwd = "hr";
	Connection conn;

	Insert insert;
	Update update;	
	Select select;
	
	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_3;
	private JMenuItem mntmAll;
	

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DatabaseTest frame = new DatabaseTest();
					//com.sun.java.swing.plaf.motif.MotifLookAndFeel
					//com.sun.java.swing.plaf.windows.WindowsLookAndFeel
					//com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
					//javax.swing.plaf.metal.MetalLookAndFeel					
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
					SwingUtilities.updateComponentTreeUI(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public DatabaseTest() {
		setTitle("데이터베이스 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 809, 617);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		//데이터베이스 연결 테스트
		try{
			//1)드라이버 로딩
			Class.forName(driver);
			System.out.println("드라이버 로딩 성공!");
			
			//2) Connection 객체 생성
			conn = DriverManager.getConnection(url, db_user, db_pwd);
			System.out.println("데이터 베이스가 정상적으로 연결됨");
		}catch(Exception ex){
			
		}
		
		
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("메뉴");
			mnNewMenu.add(getMntmAll());
			mnNewMenu.add(getMntmNewMenuItem());
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem_3());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("입력");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openInsert();
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("수정");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openUpdate();
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("조회");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openUpdate();
				}
			});
		}
		return mntmNewMenuItem_3;
	}
	private JMenuItem getMntmAll() {
		if (mntmAll == null) {
			mntmAll = new JMenuItem("All");
			mntmAll.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openInsert();
					openUpdate();
					openSelect();
				}
			});
		}
		return mntmAll;
	}
	
	public void openInsert(){
		if(insert == null){
			insert = new Insert(conn,this);
		}
		contentPane.add(insert);
		insert.setVisible(true);
		insert.toFront();
	}
	
	public void openUpdate(){
		if(update == null){
			update = new Update(conn,this);
		}
		contentPane.add(update);
		update.setVisible(true);
		update.toFront();
	}
	
	public void openSelect(){
		if(select == null){
			select = new Select(conn,this);								
		}
		contentPane.add(select);
		select.setVisible(true);
		select.toFront();
	}	
}
